package com.example.demo.crud.controller;

import com.example.demo.crud.entity.SysPersonEntity;
import com.example.demo.crud.service.SysPersonService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  前端控制器
 *
 * @author CustomGenerator
 * @since 2025-08-24 14:49:15
 */
@Slf4j
@Validated
@Api(tags = "管理")
@RestController
@RequestMapping("/crud/sys-person-entity")
public class SysPersonController {

    @Autowired
    private SysPersonService sysPersonService;

    /**
     * 分页查询列表
     */
    @ApiOperation("分页查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码", defaultValue = "1", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页大小", defaultValue = "10", dataType = "int", paramType = "query")
    })
    @GetMapping("/page")
    public ResponseEntity<Map<String, Object>> page(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            SysPersonEntity entity) {
        try {
            Page<SysPersonEntity> page = new Page<>(current, size);
            IPage<SysPersonEntity> pageResult = sysPersonService.pageList(page, entity);
            
            Map<String, Object> result = new HashMap<>();
            result.put("code", 200);
            result.put("message", "查询成功");
            result.put("data", pageResult);
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("分页查询列表失败", e);
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("message", "查询失败：" + e.getMessage());
            return ResponseEntity.status(500).body(result);
        }
    }

    /**
     * 查询列表
     */
    @ApiOperation("查询列表")
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> list(SysPersonEntity entity) {
        try {
            List<SysPersonEntity> list = sysPersonService.listByCondition(entity);
            
            Map<String, Object> result = new HashMap<>();
            result.put("code", 200);
            result.put("message", "查询成功");
            result.put("data", list);
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("查询列表失败", e);
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("message", "查询失败：" + e.getMessage());
            return ResponseEntity.status(500).body(result);
        }
    }

    /**
     * 根据ID查询详情
     */
    @ApiOperation("根据ID查询详情")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "long", paramType = "path")
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getById(@PathVariable @NotNull Long id) {
        try {
            SysPersonEntity entity = sysPersonService.getByIdWithDetails(id);
            
            Map<String, Object> result = new HashMap<>();
            if (entity != null) {
                result.put("code", 200);
                result.put("message", "查询成功");
                result.put("data", entity);
            } else {
                result.put("code", 404);
                result.put("message", "不存在");
            }
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("查询详情失败，ID: {}", id, e);
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("message", "查询失败：" + e.getMessage());
            return ResponseEntity.status(500).body(result);
        }
    }

    /**
     * 新增
     */
    @ApiOperation("新增")
    @PostMapping
    public ResponseEntity<Map<String, Object>> create(@RequestBody @Valid SysPersonEntity entity) {
        try {
            boolean success = sysPersonService.create(entity);
            
            Map<String, Object> result = new HashMap<>();
            if (success) {
                result.put("code", 200);
                result.put("message", "新增成功");
                result.put("data", entity);
            } else {
                result.put("code", 400);
                result.put("message", "新增失败");
            }
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("新增失败", e);
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("message", "新增失败：" + e.getMessage());
            return ResponseEntity.status(500).body(result);
        }
    }

    /**
     * 更新
     */
    @ApiOperation("更新")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "long", paramType = "path")
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> update(@PathVariable @NotNull Long id, @RequestBody @Valid SysPersonEntity entity) {
        try {
            entity.;
            boolean success = sysPersonService.update(entity);
            
            Map<String, Object> result = new HashMap<>();
            if (success) {
                result.put("code", 200);
                result.put("message", "更新成功");
                result.put("data", entity);
            } else {
                result.put("code", 400);
                result.put("message", "更新失败");
            }
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("更新失败，ID: {}", id, e);
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("message", "更新失败：" + e.getMessage());
            return ResponseEntity.status(500).body(result);
        }
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "long", paramType = "path")
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> delete(@PathVariable @NotNull Long id) {
        try {
            boolean success = sysPersonService.deleteById(id);
            
            Map<String, Object> result = new HashMap<>();
            if (success) {
                result.put("code", 200);
                result.put("message", "删除成功");
            } else {
                result.put("code", 400);
                result.put("message", "删除失败");
            }
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("删除失败，ID: {}", id, e);
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("message", "删除失败：" + e.getMessage());
            return ResponseEntity.status(500).body(result);
        }
    }

    /**
     * 批量删除
     */
    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    public ResponseEntity<Map<String, Object>> deleteBatch(@RequestBody @Valid List<Long> ids) {
        try {
            boolean success = sysPersonService.deleteBatchByIds(ids);
            
            Map<String, Object> result = new HashMap<>();
            if (success) {
                result.put("code", 200);
                result.put("message", "批量删除成功");
            } else {
                result.put("code", 400);
                result.put("message", "批量删除失败");
            }
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("批量删除失败", e);
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("message", "批量删除失败：" + e.getMessage());
            return ResponseEntity.status(500).body(result);
        }
    }

    /**
     * 批量新增
     */
    @ApiOperation("批量新增")
    @PostMapping("/batch")
    public ResponseEntity<Map<String, Object>> createBatch(@RequestBody @Valid List<SysPersonEntity> entityList) {
        try {
            boolean success = sysPersonService.createBatch(entityList);
            
            Map<String, Object> result = new HashMap<>();
            if (success) {
                result.put("code", 200);
                result.put("message", "批量新增成功");
                result.put("data", entityList);
            } else {
                result.put("code", 400);
                result.put("message", "批量新增失败");
            }
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("批量新增失败", e);
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("message", "批量新增失败：" + e.getMessage());
            return ResponseEntity.status(500).body(result);
        }
    }

    /**
     * 统计数量
     */
    @ApiOperation("统计数量")
    @GetMapping("/count")
    public ResponseEntity<Map<String, Object>> count(SysPersonEntity entity) {
        try {
            long count = sysPersonService.countByCondition(entity);
            
            Map<String, Object> result = new HashMap<>();
            result.put("code", 200);
            result.put("message", "统计成功");
            result.put("data", count);
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("统计数量失败", e);
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("message", "统计失败：" + e.getMessage());
            return ResponseEntity.status(500).body(result);
        }
    }

    /**
     * 检查是否存在
     */
    @ApiOperation("检查是否存在")
    @GetMapping("/exists")
    public ResponseEntity<Map<String, Object>> exists(SysPersonEntity entity) {
        try {
            boolean exists = sysPersonService.exists(entity);
            
            Map<String, Object> result = new HashMap<>();
            result.put("code", 200);
            result.put("message", "检查成功");
            result.put("data", exists);
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("检查是否存在失败", e);
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("message", "检查失败：" + e.getMessage());
            return ResponseEntity.status(500).body(result);
        }
    }
}
